/*
 * Copyright (c) 2011, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.registry.lifecycle.accenture.sample.internal;

import org.wso2.carbon.user.api.RealmConfiguration;
import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.service.RealmService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @scr.component name="user.store.services.dscomponent" immediate="true"
 * @scr.reference name="user.realmservice.default"
 *                interface="org.wso2.carbon.user.core.service.RealmService"
 *                cardinality="1..1" policy="dynamic" bind="setRealmService"
 *                unbind="unsetRealmService"
 */

public class UserStoreServiceComponent {
    private static final Log log = LogFactory.getLog(UserStoreServiceComponent.class);

    private static RealmService realmService;

     protected void setRealmService(RealmService realmService) {
        UserStoreServiceComponent.realmService = realmService;
    }

    protected void unsetRealmService(RealmService realmService) {
        UserStoreServiceComponent.realmService = null;
    }

     public static RealmService getRealmService() {
        if (realmService == null) {
            String msg = "System has not been started properly. Realm Service is null.";
            log.error(msg);
        }
        return realmService;
    }

    public static RealmConfiguration getRealmConfig() {
        if (realmService == null) {
            String msg = "System has not been started properly. Realm Service is null.";
            log.error(msg);
        }
        return realmService.getBootstrapRealmConfiguration();
    }

    public static UserStoreManager getUserStoreManager() {
        if (realmService == null) {
            String msg = "System has not been started properly. Realm Service is null.";
            log.error(msg);
        }
        try {
            return realmService.getBootstrapRealm().getUserStoreManager();
        } catch (UserStoreException e) {
            log.error(e);
        }
        return null; //TODO: Fix this null properly
    }
}
